package com.kotlin.network.retrofit.http;

import android.content.Context;
import com.kotlin.network.ErrorCode;
import com.kotlin.network.retrofit.NetWorkResponse;
import com.orhanobut.logger.Logger;
import rx.functions.Func1;

/**
 * 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber
 *
 * @param <T> Subscriber真正需要的数据类型，也就是Data部分的数据类型
 */
public class HttpResultFunc<T> implements Func1<NetWorkResponse<T>, T> {

    private String tag = "";// 标识区分哪个请求，以便特殊处理

    private Context context;

    public HttpResultFunc() {
    }
    public HttpResultFunc(String tag) {
        this.tag = tag;
    }

    @Override
    public T call(NetWorkResponse<T> httpResult) {
        if (httpResult.getCode()== ErrorCode.OK) {

            //Logger.init("httpResult");
            //Logger.d("Message = " + String.valueOf(httpResult.getData()));

            if (httpResult.getData() == null || String.valueOf(httpResult.getData()).equals("{}") || httpResult.getData().equals("")) {
                return null;

                //Log.d("super","msg:"+httpResult.getMessage());
                //Logger.init("httpResult");
                //Logger.d("Message = " + httpResult.getMsg() +httpResult.getCode());

                //如果返回正确情况，就返回正确的String 对像
                //return (T) (httpResult.getMsg());
            }
            else
            {
                Logger.init("httpResult");
                Logger.d("Message = " + httpResult.getData());
                return httpResult.getData();
            }



        } else {

            throw new ApiException(httpResult.getCode(), httpResult.getMsg());
        }
    }
}
